﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.SessionState;
using System.IO;
using FmcWebsite.Core;

namespace FmcWebsite.Web
{
    public class WebAttachmentAudio
    {
        private AttachmentAUDIO audio;

        private string pickup_dir = @"c:\wwwfmc\audio\";//@"d:\Projects\FmcWebsite\Cms\audio\";
        private string pickup_url = @"\audio\";

        private WebAttachmentAudio(AttachmentAUDIO audio, HttpSessionState session)
        {
            this.audio = audio;
        }

        public string ContentType
        {
            get { return audio.ContentType; }
        }

        private string GetFileName()
        {
            return string.Format("audio{0}_{1}.mp3", audio.Id, audio.Blob.Length);
        }

        public string GetUrl()
        {
            // saving to pickup dir
            string filename = pickup_dir + GetFileName();
            if (!File.Exists(filename))
            {
                FileStream file = new FileStream(filename, FileMode.Create);
                try
                {
                    Save(file);
                }
                finally
                {
                    file.Close();
                }
            }
            return pickup_url + GetFileName();
        }

        public long Save(Stream stream)
        {
            return audio.Save(stream);
        }

        static public WebAttachmentAudio ById(int att_id, HttpSessionState session)
        {
            Attachment att = Attachment.ById(att_id);
            if (att is AttachmentAUDIO)
            {
                return new WebAttachmentAudio((AttachmentAUDIO)att, session);
            }
            return null;
        }
    }
}
